/**
 * 
 */
package com.wxq.weitool;

import java.util.Comparator;
import java.util.function.Function;

/**
 * @author West.W
 * create date 2018-02-09 13:52:32
 */

public class Comparators {
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  int compare(Comparable left, Comparable right ) {
		
		if (left == null) {
			if (right == null) {
				return 0;
			} else {
				return -1;
			}
		}else {
			if (right == null) {
				return 1;
			} else {
				return left.compareTo(right);
			}
		} 
	}
	

	/**bean比较器  排序字段需comparable
	 * @param getter bean的排序字段的getter
	 * @return
	 */
	public static <T> Comparator<T> beanComparator(Function<T, Comparable<?>> getter ) {
		return (a,b)->compare(getter.apply(a), getter.apply(b));
	}
	
}
